// pages/user/job_auth.js
import {User} from "../../model/user";
import {config} from "../../config/config";
import {Auth} from "../../model/auth";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    status: 1,
    job_img: null,
    company_name: '',
    btn_loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.showLoading({
      title: '加载中',
    });
    this.getUserInfo();
  },
  /**
   * 获取用户信息
   */
  async getUserInfo() {
    const res = await User.getUserInfo();
    if(res.auth_job) {
      this.setData({
        status: res.auth_job.status,
        job_img: res.auth_job.job_img,
        company_name: res.auth_job.company_name,
      });
    }
    this.setData({
      userInfo: res
    });
    wx.hideLoading();
  },
  /**
   * 上传图片
   */
  chooseImg() {
    if(this.data.status == 2) {
      return false;
    }
    wx.showLoading({
      title: '加载中',
    });
    const self = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        wx.uploadFile({
          url: config.apiFileUrl, // 仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[0],
          name: 'file',
          success(res) {
            const fileData = JSON.parse(res.data);
            self.setData({
              job_img: fileData[0]
            });
            wx.hideLoading();
          },
        });
      }
    })
  },
  /**
   * 创建信息
   */
  async createUserAuth() {
    if(!this.data.job_img) {
      wx.showToast({
        title: '请上传工作证明',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      btn_loading: true
    })
    const res = await Auth.createUserJob({job_img_id: this.data.job_img.id, company_name: this.data.company_name});
    if(res) {
      wx.showToast({
        title: res.msg,
        icon: 'none',
        duration: 2000
      })
    }
    this.getUserInfo();
    this.setData({
      btn_loading: false
    })
  },
  /**
   * 更改名称
   */
  bindInputName(e) {
    this.setData({
      company_name: e.detail.value
    })
  }
})